<?php

namespace App\Dao;

use App\Model\AppUpgrade;
use App\Model\AuditIp;
use App\Model\AuditIpSegment;
use App\Model\Forbidden;
use App\Model\Order;
use App\Model\User;
use App\Utils\FunUtil;
use App\Utils\LoggerUtil;
use function Hyperf\Config\config;

class AppUpgradeDao
{

    /**
     * 查询版本配置信息
     * @param $from
     * @param $version_code
     * @param $channel
     * @param string $ip
     * @param int $uid
     * @return mixed|string[]
     */
    public function getSetUp($from, $version_code, $channel, string $ip = "127.0.0.1", int $uid = 0)
    {
        $fieldArr = [
            "hide_guide_page",
            "hide_call",
            "hide_male_vip",
            "hide_robot_chat",
            "hide_female_wechat",
            "hide_female_private_photo",
            "hide_female_private_video",
            "hide_female_chat_real_auth",
            "hide_male_chat_cost",
            "hide_first_recharge",
            "hide_share_getfee_btn",
            "show_female_send_gift",
            "show_female_charge",
            "show_male_withdraw",
            "show_male_look_history",
            "show_same_sex_comment",
            "open_login_device",
            //"hide_invite_code",
        ];

        //+++++++++++++++++++++++++++++++++++++++欢遇官方包不进入审核环境++++++++++++++++++++++++++++++++++++++++++++++++++++//
        if ($channel == 'xunyuan_gf') {
            if ($uid) {
                // in_audit:在审核环境列表 0否 1是
                // is_audit:审核环境注册用户 0否 1是
                // login_env:登录环境 0正常 1审核环境
                // $this->updateByWhere(['in_audit' => 0, 'is_audit' => 0, 'login_env' => 0], "id={$uid}", "tb_user");
                User::query()->where('id', $uid)->update(['login_env' => 0, 'in_audit' => 0, 'is_audit' => 0]);
            }
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 0;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //+++++++++++++++++++++++++++++++++++++++欢遇官方包不进入审核环境++++++++++++++++++++++++++++++++++++++++++++++++++++//


        //+++++++++++++++++++++++++++++++++++++++配置审核环境++++++++++++++++++++++++++++++++++++++++++++++++++++//
        if (config("envname") == 'audit' || in_array($uid, AUDIT_UIDS) || $channel == 'baibao') {
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 1;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //+++++++++++++++++++++++++++++++++++++++配置审核环境++++++++++++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++当前用户是白名单用户，不进入审核环境+++++++++++++++++++++++++++++++++++++++++++//
        $auditWhite = 0;
        $user = User::query()->select('audit_white', 'sex', 'register_time', 'inviteUid')->where('id', $uid)->first();
        if (!empty($user)) {
            // audit_white:审核环境白名单 0否 1是
            if ($user['audit_white'] || $user['inviteUid'] > 0) {
                $auditWhite = 1;
            } elseif ($user['sex'] == 1) {
                // 男用户充值过的 不进入审核环境
                // $order = $this->find("id,order_id", "uid=? and pay_status=1 and notify_status=1", [$uid], '', 'tb_order');
                $order = Order::query()->select('id', 'order_id')->where('uid', $uid)->where('pay_status', 1)->where('notify_status', 1)->first();
                if (!empty($order)) {
                    $auditWhite = 1;
                }
            }
        }
        if ($auditWhite) {
            if ($uid) {
                // $this->updateByWhere(['in_audit' => 0, 'is_audit' => 0, 'login_env' => 0], "id={$uid}", "tb_user");
                User::query()->where('id', $uid)->update(['login_env' => 0, 'in_audit' => 0, 'is_audit' => 0]);
            }
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 0;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //++++++++++++++++++++++++++++++++++++++++当前用户是白名单用户，不进入审核环境+++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++当前用户在审核环境注册登录，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//
        if ($uid) {
            $user = User::query()->select('id', 'login_env', 'in_audit', 'is_audit')->where('id', $uid)->first();
            if ($user['login_env'] == 1 || $user['in_audit'] == 1 || $user['is_audit'] == 1) {
                foreach ($fieldArr as $k => $v) {
                    $fieldArr[$v] = 1;
                    unset($fieldArr[$k]);
                }
                return $fieldArr;
            }
        }
        //++++++++++++++++++++++++++++++++++++++++当前用户在审核环境注册登录，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++ip白名单，不进入审核环境+++++++++++++++++++++++++++++++++++++++++++//
        $ip = $ip === "127.0.0.1" ? FunUtil::getClientIp() : $ip;
        // ip白名单直接返回
        $auditIpModel = new AuditIp();
        $isWhiteIp = $auditIpModel->isWhiteIp($ip);
        if ($isWhiteIp) {
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 0;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //++++++++++++++++++++++++++++++++++++++++ip白名单，不进入审核环境+++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++审核环境IP，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//
        $inAuditIp = $auditIpModel->isAuditIp($ip);
        // 华为、vivo、oppo市场审核条件
        $result = (new AuditIpSegment())->isAuditIp($ip, $channel);
        if ($result) {
            $inAuditIp = 1;
        }

        if ($inAuditIp) {
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 1;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //++++++++++++++++++++++++++++++++++++++++审核环境IP，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++屏蔽地区，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//
        $forbiddenCity = Forbidden::forbiddenCity($ip);
        if ($forbiddenCity) {
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 1;
                unset($fieldArr[$k]);
            }
            return $fieldArr;
        }
        //++++++++++++++++++++++++++++++++++++++++屏蔽地区，进入审核环境+++++++++++++++++++++++++++++++++++++++++++//


        //++++++++++++++++++++++++++++++++++++++++根据当前配置+++++++++++++++++++++++++++++++++++++++++++//
        $rs = AppUpgrade::query()->select($fieldArr)->where('type', $from)->where('version_code', $version_code)->where('channel', $channel)->first();
        if (!$rs) {
            foreach ($fieldArr as $k => $v) {
                $fieldArr[$v] = 0;
                unset($fieldArr[$k]);
            }
            $rs = $fieldArr;
        }
        foreach ($rs as $k => $v) {
            $rs[$k] = intval($v);
        }
        return $rs;
        //++++++++++++++++++++++++++++++++++++++++根据当前配置+++++++++++++++++++++++++++++++++++++++++++//

    }


    public function isHideFirstRecharge($from, $version_code, $channel, $ip = "127.0.0.1")
    {
        $rs = $this->getSetUp($from, $version_code, $channel, $ip);
        if ($rs) {
            return boolval($rs["hide_first_recharge"]);
        }
        return false;
    }

    // 查询是否屏蔽视屏通话
    public function isHideCall($from, $version_code, $channel, $ip = "127.0.0.1")
    {
        LoggerUtil::write('appup', $from . "|" . $version_code . "|" . $channel . "|" . $ip);
        $rs = $this->getSetUp($from, $version_code, $channel, $ip);
        if ($rs) {
            return boolval($rs["hide_call"]);
        }
        return false;
    }


    // 查询是否屏蔽机器人拨打
    public function isHideRobotChat($from, $version_code, $channel, $ip = "127.0.0.1")
    {
        $rs = $this->getSetUp($from, $version_code, $channel, $ip);
        if ($rs) {
            return boolval($rs["hide_robot_chat"]);
        }
        return false;
    }

    // 查询是否屏蔽男用户付费聊天
    public function isHideMaleChatCost($from, $version_code, $channel, $ip = "127.0.0.1", $uid = 0)
    {
        try {
            $rs = $this->getSetUp($from, $version_code, $channel, $ip, $uid);
            if ($rs) {
                return boolval($rs["hide_male_chat_cost"]);
            }
            return false;
        } catch (\Throwable $e) {
            LoggerUtil::write("appup", $e->getMessage());
        }

    }

    // 获取审核中的APP
    public function getAuditingList()
    {
        return AppUpgrade::query()->select(["type", "version_code", "channel"])->where("is_auditing", 1)->get()->toArray();
    }

    /**
     * 是否允许同性评论、聊天
     * @param $from
     * @param $version_code
     * @param $channel
     * @param string $ip
     * @return bool
     */
    public function isShowSameSexComment($from, $version_code, $channel, string $ip = "127.0.0.1")
    {
        $rs = $this->getSetUp($from, $version_code, $channel, $ip);
        if ($rs) {
            return boolval($rs["show_same_sex_comment"]);
        }
        return false;
    }

}